package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserService implements IUserService {

    @Autowired
    private IUserMapper mapper;
    @Override
    public boolean register(UserBean userBean) {
        // 根据用户账号查询用户对象
        QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_name", userBean.getUsername());
        UserBean user = mapper.selectOne(queryWrapper);
        // 判断用户是否已存在
        if (user != null) {
            // 用户已存在，注册失败
            return false;
        } else {
            // 用户不存在，进行注册操作
            mapper.insert(userBean);
            return true;
        }
    }

    @Override
    public UserBean login(String name, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name",name);
        qw.eq("u_pwd",pwd);

        return mapper.selectOne(qw);
    }
}
